﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Modules;
using WP.DB;
using WP.Controls;
using WP.Commons;
using System.Collections;


namespace WP.IM.Form
{
    public partial class frmImCategoryWKRatingCopy : BaseQryRpt
    {
        private DBAccess mdbcon = new DBAccess(false, false);
        private bool mblnPressExit = true;
        private string mstrFItemID = "";
        public frmImCategoryWKRatingCopy()
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
            this.MinimumSize = this.Size;
        }

        private void LookUp_Click(object sender, EventArgs e)
        {
            string strFindValue = string.Empty;
            string strFieldName = string.Empty;
            string strSql = string.Empty;
            string strCaption = string.Empty;
            Control con = sender as Control;
            if (con == null) return;
            Control ocon = null;
            switch (con.Name)
            {
                case "ubtWKName":
                    strFindValue = txtCategoryName.Text;
                    strFieldName = "WKProductID";
                    strCaption = "工序 选择";
                    strSql = BaseFunctions.GetLookUpSQL("rmCategoryWK");
                    ocon = txtWKID ;
                    break;

                case "ubtCategoryName":
                    strFindValue = txtCategoryName.Text;
                    strFieldName = "CategoryName";
                    strCaption = "类别 选择";
                    strSql = BaseFunctions.GetLookUpSQL("rmCategoryWK");
                    ocon = txtCategoryName;
                    break;
            }
            if (mblnLookUpSortByDescription) strSql = DataFunctions.GetSqlByDescriptionSort(strSql);
            mblnLookUpSortByDescription = false;

            frmLookUp frmlookup = new frmLookUp(strSql, strCaption, strFindValue, strFieldName);
            frmlookup.ShowDialog();
            if (frmlookup.HaveReturnValue)
            {
                txtCategoryID.Text = frmlookup.GetReturnValue("CategoryID");
                txtCategoryName.Text = frmlookup.GetReturnValue("CategoryName");
                txtWKID.Text = frmlookup.GetReturnValue("WKProductID");
                txtWKName.Text = frmlookup.GetReturnValue("WKName");
                ocon.Focus();
            }
            frmlookup.Close();
        }

        public bool ShowCopyItem(ref string strNewCategoryID,ref string strNewCategoryName, ref string strNewWKID,ref string strNewWKName,string strFItemID)
        {
            txtCategoryID.Visible = false;
            txtCategoryName.Visible = false;
            ubtCategoryName.Visible = false;
            label1.Visible = false;
            this.Text = "复制到新物料";
            mstrFItemID = strFItemID;
            this.ShowDialog();
            if (!mblnPressExit)
            {
                //strNewCategoryID = txtCategoryID.Text;
                strNewWKID = txtWKID.Text;
                //strNewCategoryName = txtCategoryName.Text;
                strNewWKName = txtWKName.Text ;
                return true;
            }
            return false;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string strErrMsg = "";
            if (VaildCheck(ref strErrMsg))
            {
                mblnPressExit = false;
                this.Close();
            }
            else
            {
                if (strErrMsg != "")
                {
                    MessageBox.Show(strErrMsg, SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private bool VaildCheck(ref string strErrMsg)
        {
            if (txtWKID.Text.Trim() == "")
            {
                strErrMsg = "工序不能为空。";
                return false;
            }
            string strSQL = "select * from ImCategoryWKRating where  WKProductID='{0}' and CategoryID='{1}' and fitemid='{2}' ";
            strSQL = string.Format(strSQL, txtWKID.Text, txtCategoryID.Text,mstrFItemID);
            DataRow drCheck = mdbcon.GetDataFirstRow(strSQL);
            if (drCheck != null)
            {
                strErrMsg = "此设置已经存在。请检查。";
                return false;
            }
            return true;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            mblnPressExit = true;
            this.Close();
        }

    }
}
